package com.itheima.commons.utils;

import cn.hutool.json.JSONUtil;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.moderation.v2.ModerationClient;
import com.huaweicloud.sdk.moderation.v2.model.*;
import com.huaweicloud.sdk.moderation.v2.region.ModerationRegion;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author yaohu
 * @date 2021/12/18
 * @Version 1.0
 * @describe
 */
public class ContentCheckUtil {

    private static String ak;
    private static String sk;
    private static String regionld;
    private static ModerationClient client;

    static {
        Properties properties = new Properties();
        try {
            properties.load(ContentCheckUtil.class.getClassLoader().getResourceAsStream("huawei.properties"));
            ak = properties.getProperty("HUAWEI_AK");
            sk = properties.getProperty("HUAWEI_SK");
            regionld = properties.getProperty("HUAWEI_regionld");
        } catch (IOException e) {
            e.printStackTrace();
        }

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        client = ModerationClient.newBuilder()
                .withCredential(auth)
                .withRegion(ModerationRegion.valueOf(regionld))
                .build();
    }

    public static String textCheck(String text) {
        RunTextModerationRequest request = new RunTextModerationRequest();
        TextDetectionReq body = new TextDetectionReq();
        List<TextDetectionItemsReq> listbodyItems = new ArrayList<>();
        listbodyItems.add(
                new TextDetectionItemsReq()
                        .withText(text)
                        .withType("content")
        );
        List<String> listbodyCategories = new ArrayList<>();
        listbodyCategories.add("ad");
        listbodyCategories.add("politics");
        listbodyCategories.add("abuse");
        listbodyCategories.add("porn");
        listbodyCategories.add("contraband");
        listbodyCategories.add("flood");
        body.withItems(listbodyItems);
        body.withCategories(listbodyCategories);
        request.withBody(body);
        RunTextModerationResponse response = null;
        //审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝
        try {
            response = client.runTextModeration(request);
            System.out.println(response.getHttpStatusCode());
            System.out.println(response);
            if ("block".equals(response.getResult().getSuggestion())){
                return "6";
            } else if ("review".equals(response.getResult().getSuggestion())) {
                return "3";
            } else if ("pass".equals(response.getResult().getSuggestion())) {
                return "2";
            }
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return "1";
    }

    /**
     * 批量图片审核
     *
     * @param listbodyUrls 图片地址集合
     * @return
     */
    public static String imagesCheck(List<String> listbodyUrls) {
        RunImageBatchModerationRequest request = new RunImageBatchModerationRequest();
        ImageBatchModerationReq body = new ImageBatchModerationReq();
        List<ImageBatchModerationReq.CategoriesEnum> listbodyCategories = new ArrayList<>();
        listbodyCategories.add(ImageBatchModerationReq.CategoriesEnum.fromValue("politics"));
        listbodyCategories.add(ImageBatchModerationReq.CategoriesEnum.fromValue("terrorism"));
        listbodyCategories.add(ImageBatchModerationReq.CategoriesEnum.fromValue("porn"));
        body.withThreshold((double) 0);
        body.withCategories(listbodyCategories);
        body.withUrls(listbodyUrls);
        request.withBody(body);
        RunImageBatchModerationResponse response = null;
        //审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝
        try {
            response = client.runImageBatchModeration(request);
            System.out.println(response.getHttpStatusCode());
            System.out.println(JSONUtil.toJsonStr(response));
            for (ImageBatchModerationResultBody resultBody : response.getResult()) {
                if ("block".equals(resultBody.getSuggestion())) {
                    return "6";
                } else if("review".equals(resultBody.getSuggestion())) {
                    return "3";
                } else {
                    return "2";
                }
            }
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return "1";
    }
}
